package com.iap.admin.service
{
	import com.iap.admin.event.TagEvent;
	import com.iap.app.data.AppData;
	import com.iap.app.data.IAppData;
	import com.iap.app.model.ServiceResponse;
	import com.iap.common.model.Tag;
	import com.iap.common.translator.TagTranslator;
	
	import org.robotlegs.mvcs.Actor;
	
	public class TagService extends Actor implements ITagService
	{
		[Inject]
		public var data:IAppData;

		private var _task:String;
		public function set task(value:String):void { _task = value; }

		public function getList():void
		{
			data.dataComplete.add(responseHandler);
			data.load(this, AppData.JOB_GET);
		}
		
		public function save(model:Tag):void
		{
			data.dataComplete.add(responseHandler);
			data.load(this, AppData.JOB_SAVE, model);
		}
		
		public function remove(model:Tag):void
		{
			data.dataComplete.add(responseHandler);
			data.load(this, AppData.JOB_REMOVE, model);
		}

		private function responseHandler(response:ServiceResponse):void
		{
			data.dataComplete.remove(responseHandler);
			response.task = _task;
			response.result = TagTranslator.translate(response.rawdata);
			dispatch(new TagEvent(TagEvent.RESPONSE, response));
		}
	}
}